package org.feng.clog.config;

import org.feng.clog.utils.StringUtil;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * log拦截器
 *
 * @author feng
 */
@Component
@Order(-100)
public class LogInterceptor implements HandlerInterceptor {

    private static final String LOG_ID = "logId";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String logId = request.getHeader(LOG_ID);
        if (StringUtil.isNotBlank(logId)) {
            MDC.put(LOG_ID, logId);
        } else {
            MDC.put(LOG_ID, StringUtil.uuid());
        }
        return true;
    }
}
